/* eslint-disable indent */
'use strict'

/**
 * 会员信息表
 */

module.exports = app => {
    const { TEXT, STRING, INTEGER, BIGINT, DATE, JSON } = app.Sequelize

    const Member = app.model.define('member', {
        id: {
            type: INTEGER,
            primaryKey: true,
            autoIncrement: true,
            comment: 'ID'
        },
        mid: {
            type: STRING,
            allowNull: false,
            comment: '会员ID'
        },
        openid: {
            type: STRING,
            allowNull: true,
            comment: '微信openid'
        },
        unionid: {
            type: STRING,
            allowNull: true,
            comment: '微信unionid'
        },
        phone: {
            type: STRING,
            allowNull: true,
            comment: '手机号'
        },
        nickname: {
            type: STRING,
            allowNull: true,
            comment: '昵称'
        },
        avatar: {
            type: STRING,
            allowNull: true,
            comment: '头像'
        },
        sex: {
            type: INTEGER,
            allowNull: true,
            comment: '性别'
        },
        birthday: {
            type: STRING,
            allowNull: true,
            comment: '生日'
        },
        register_time: {
            type: DATE,
            allowNull: true,
            comment: '注册时间'
        },
        last_login_time: {
            type: DATE,
            allowNull: true,
            comment: '最后上机时间'
        },
        level_name: {
            type: STRING,
            allowNull: true,
            comment: '会员等级名称'
        },
        subscribe: {
            type: INTEGER,
            allowNull: true,
            comment: '是否关注'
        },
        subscribe_time: {
            type: DATE,
            allowNull: true,
            comment: '关注时间'
        },
        pid: {
            type: INTEGER,
            allowNull: false,
            comment: '门店ID'
        },
        card_id: {
            type: STRING,
            allowNull: true,
            comment: '卡号'
        },
        balance: {
            type: INTEGER,
            allowNull: true,
            comment: '余额',
            defaultValue: 0
        },
        recharge_amount: {
            type: INTEGER,
            allowNull: true,
            comment: '累计充值金额',
            defaultValue: 0
        },
        consume_time: {
            type: INTEGER,
            allowNull: true,
            comment: '累计上机时长（分钟）',
            defaultValue: 0
        },
        week_count: {
            type: INTEGER,
            allowNull: true,
            comment: '本周上机消费次数',
            defaultValue: 0
        },
        count: {
            type: INTEGER,
            allowNull: true,
            comment: '上机消费次数',
            defaultValue: 0
        },
        status: {
            type: INTEGER,
            allowNull: true,
            comment: '状态',
            defaultValue: 1
        }
    })


    // 建立关联关系
    Member.associate = async () => {
        Member.belongsTo(app.model.Place, {
            onDelete: 'RESTRICT',
            onUpdate: 'CASCADE',
            foreignKey: 'pid', // 源模型中的外键信息
            // as: 'place', // 添加到源模型中的目标模型别名
            targetKey: 'id' // 目标模型中的关联字段
        })
    }

    return Member
}
